﻿App.Assigner = function() {
    return {

        currentFormValues: {},

        getStore: function() {
            var store = new Ext.data.Store({
                autoLoad: {
					params : {start:0, limit:15}
				},
				proxy: new Ext.data.HttpProxy({
					url : 'cost/items'
				}),
                remoteSort: false,
				reader: new Ext.data.JsonReader({
					totalProperty : 'totalCount',
					root : 'rows',
					idProperty : 'cost_id',
					fields: [{
						name: 'today',
						type: 'string'
					},
					{
						name: 'last_week',
						allowBlank: false
					},
					{
						name: 'last_month',
						allowBlank: false
					}]
				})
            });
            return store;
        },



        submit: function() {
            if (this.form.getForm().isValid()) {
                this.form.getForm().submit({
                    waitTitle: '保存数据',
                    waitMsg: '正在保存……',
                    scope: this,
                    method: 'post',
                    params: '',
                    success: function(form, action) {
                       var info = action.result.info;
                        if (action.result.success) {
                            this.store.reload();
                            if (action.result.method == "Create") {
                                this.form.getForm().reset();
                            } else {
                                this.dlg.hide();
                            }
                        }
                        Ext.Msg.alert('信息', info);
                    },
                    failure: function(form, action) {
                        var info = action.result.info;
	    				Ext.Msg.alert('提示', info);
                    }
                });
            }
        },


        createGrid: function(id) {

            var panel = Ext.getCmp(id);

            panel.body.dom.innerHTML = "";
            var sm = new Ext.grid.CheckboxSelectionModel();

            this.grid = new Ext.grid.GridPanel({
                loadMask: true,
                tbar: [{
                    text: '增加',
                    scope: this,
                    handler: this.add
                },
                {
                    text: '编辑',
                    scope: this,
                    handler: this.edit
                },
                {
                    text: '删除',
                    scope: this,
                    handler: this.del
                }],
                store: this.store,
                sm: sm,
                columns: [sm, {
                    header: "当天营收汇总",
                    width: 60,
                    sortable: true,
                    dataIndex: 'today'
                },
                {
                    header: "过去一周汇总",
                    width: 100,
                    sortable: true,
                    dataIndex: 'last_week'
                },
                {
                    header: "过去一月汇总",
                    width: 100,
                    sortable: true,
                    dataIndex: 'last_month'
                }]
            });
            panel.add(this.grid);
            var displayPanel = new Ext.Panel({
        		width        : 650,
        		height       : 300,
        		layout       : 'hbox',
        		renderTo     : 'panel',
        		defaults     : { flex : 1 }, //auto stretch
        		layoutConfig : { align : 'stretch' },
        		items        : [
        			
        		]});

        },
        getForm: function() {
            var form = new Ext.form.FormPanel({
				url:'inhouse/save',
                labelAlign: 'left',
                buttonAlign: 'center',
                bodyStyle: 'padding:5px',
                frame: true,
                labelWidth: 80,
                defaultType: 'textfield',
                defaults: {
                    allowBlank: true,
                    anchor: '90%',
                    enableKeyEvents: true
                },
                items: [{
                    xtype: 'hidden',
                    name: 'id',
                    value: ''
                },{
                	 name: 'object_id',
                     fieldLabel: '物件单号'
                },
                {
                    name: 'from_code',
                    fieldLabel: '发件邮编'
                },{
                    name: 'from_addr',
                    fieldLabel: '发件地址'
                },
                {
                	name: 'from_name',
                    fieldLabel: '发件人'
                },
                {
                	name:'from_tell',
                	xtype : 'numberfield',
                    fieldLabel: '发件人电话'
                },
                {
                    name: 'from_cmp',
                    fieldLabel: '发件人公司'
                },
                {
                    name: 'to_code',
                    fieldLabel: '收件人邮编'
                },
                {
                	name: 'to_addr',
                    fieldLabel: '收件地址'
                }, {
                	name: 'to_name',
                    fieldLabel: '收件人姓名'
                },
                {
                	name: 'to_tell',
                	xtype : 'numberfield',
                    fieldLabel: '收件人电话'
                },
                {
                	name: 'to_cmp',
                    fieldLabel: '收件人公司'
                },
                {
                    name: 'emerg',
                    fieldLabel: '紧急联系人'
                },
                {
                    name: 'ppice',
                    fieldLabel: '是否保价'
                },
                {
                    name: 'price',
                    fieldLabel: '保价金额'
                },
                {
                    name: 'brief',
                    fieldLabel: '简介'
                },
                {
                    xtype: 'panel',
                    border: false,
                    html: "<span id='pwdMsg' style='color:red;'></span>"
                }],
                buttons: [{
                    text: '确定',
                    scope: this,
                    handler: function() {
                        this.submit();
                    }
                },
                {
                    text: '重置',
                    scope: this,
                    handler: function() {
                        var f = this.form.getForm();
                        f.reset();
                        f.setValues(this.currentFormValues);
                        f.clearInvalid();
                    }
                }]
            });
            return form;
        },
        getDialog: function() {
            var dlg = new Ext.Window({
                width: 400,
                height: 560,
                title: '',
                plain: true,
                closable: true,
                resizable: false,
                frame: true,
                layout: 'fit',
                closeAction: 'hide',
                border: false,
                modal: true,
                items: [this.form],
                listeners: {
                    scope: this,
                    render: function(fp) {
                        this.form.getForm().waitMsgTarget = fp.getEl();
                    },
                    show: function() {
                        var f = this.form.getForm();
                 //       f.findField("roleList").store.reload();
                        f.setValues(this.currentFormValues);
                        f.clearInvalid();
                        if (this.currentFormValues.id) {
                            Ext.get("pwdMsg").dom.innerHTML = "<span style='color:red;line-height:30px;'>*如果不修改密码请不要输入密码和确认密码</span>";
                            f.findField("password").allowBlank = true;
                            f.findField("confirmPassword").allowBlank = true;
                        } else {
                            f.findField("password").allowBlank = false;
                            f.findField("confirmPassword").allowBlank = false;
                            Ext.get("pwdMsg").dom.innerHTML = "<span style='color:red;'></span>";
                        }
                    }
                }
            });
            return dlg;
        },
        add: function() {
            Ext.apply(this.currentFormValues, {
                id: '',
                name: "",
                password: "",
                confirmPassword: "",
                roleList: ""
            });
            this.dlg.setTitle("增加物件");
            this.dlg.show();
        },

        edit: function() {
            if (this.grid.getSelectionModel().hasSelection()) {
                this.dlg.setTitle("编辑用户");
                var rec = this.grid.getSelectionModel().getSelected();
                var ml = [];
                for (var i = 0; i < rec.data.roles.length; i++) {
                    ml.push(rec.data.roles[i].staff_id);
                }
                Ext.apply(this.currentFormValues, {
                	staff_id: rec.data.id,
                    name: rec.data.name,
                    password: "",
                    confirmPassword: "",
                    roleList: ml.toString()
                });
                this.dlg.show();
            } else {
                Ext.Msg.alert('信息', '请选择要编辑的用户。');
            }
        },

        del: function() {
            if (this.grid.getSelectionModel().hasSelection()) {
                var recs = this.grid.getSelectionModel().getSelections();
                var ids = [];
                var titles = '';
                for (var i = 0; i < recs.length; i++) {
                    var data = recs[i].data;
                    ids.push(data.staff_id);
                    titles += data.name + '<br>';
                }
                Ext.Msg.confirm('删除用户', '确定删除以下用户？<br><font color="red">' + titles + '</font>',
                function(btn) {
                    if (btn == 'yes') {
						Ext.Ajax.request({
							method:'post',
							url:'user/remove',
							params:{
								ids:ids.toString()
							},
							success: function(resp, opts) {
								var result = Ext.util.JSON.decode(resp.responseText);
								var info = result.info;
								if(result.success=='true') {
									Ext.Msg.alert('信息', info);
                                    App.User.store.reload();
								}else {
                                    Ext.Msg.alert('信息', info);
                                }
							},
							failure: function(resp,opts) {
								var result = Ext.util.JSON.decode(resp.responseText);
								var info = result.info;
								Ext.Msg.alert('提示', info); 
							}
						});
                    }
                });
            } else {
                Ext.Msg.alert('信息', '请选择要删除的用户！');
            }
        },


        render: function(id) {
            if (!this.store) {
                this.store = this.getStore();
            };
            if (!this.form) {
                this.form = this.getForm();
            };
            if (!this.dlg) {
                this.dlg = this.getDialog();
            };
            this.createGrid(id);

        }
    };
} ();
